/**
* @project admin-go-api
* @name sysRole
* @descriptions 角色服务层
* @author 寒锋
* @date 2023-12-20 11:26:22
 */

package service

import (
	"admin-go-api/api/dao"
	"admin-go-api/api/entity"
	"admin-go-api/common/response"
	"admin-go-api/constants"
	"fmt"
	"github.com/gin-gonic/gin"
)

// ISysRoleService 定义角色接口
type ISysRoleService interface {
	GetSysRoleList(c *gin.Context, PageNum int, PageSize int, RoleName string, RoleKey string, Status int, BeginTime string, EndTime string) // 角色列表
	GetSysRoleVoList(c *gin.Context)                                                                                                         // 角色下拉列表
	GetSysRoleMenuIdList(c *gin.Context, RoleId int)                                                                                         // 根据角色id获取菜单id
	CreateSysRole(c *gin.Context, sysRole entity.StoreSysRoleDto)                                                                            // 添加角色
	GetSysRoleById(c *gin.Context, RoleId int)                                                                                               // 角色详情
	UpdateSysRole(c *gin.Context, sysRole entity.UpdateSysRoleDto)                                                                           // 更新角色
	UpdateSysRoleState(c *gin.Context, sysRole entity.UpdateSysRoleStateDto)                                                                 // 修改角色状态
	DeleteSysRoleById(c *gin.Context, dto entity.SysRoleIdDto)                                                                               // 删除角色
	AssignPermissions(c *gin.Context, roleMenu entity.RoleMenu)                                                                              // 分配权限
}

type SysRoelServiceImpl struct {
}

// GetSysRoleList 角色列表
func (s SysRoelServiceImpl) GetSysRoleList(c *gin.Context, PageNum int, PageSize int, RoleName string, RoleKey string, Status int, BeginTime string, EndTime string) {
	if PageSize < 1 {
		PageSize = 10
	}
	if PageNum < 1 {
		PageNum = 1
	}
	list, count := dao.GetSysRoleList(PageNum, PageSize, RoleName, RoleKey, Status, BeginTime, EndTime)
	data := map[string]interface{}{"total": count, "pageNum": PageNum, "pageSize": PageSize, "rows": list}
	response.Success(c, data)
}

// GetSysRoleVoList 角色下拉列表
func (s SysRoelServiceImpl) GetSysRoleVoList(c *gin.Context) {
	list := dao.QuerySysRoleVoList()
	response.Success(c, list)
}

// GetSysRoleMenuIdList 获取角色菜单
func (s SysRoelServiceImpl) GetSysRoleMenuIdList(c *gin.Context, RoleId int) {
	menuIds := dao.QueryRoleMenuIdList(RoleId)
	response.Success(c, menuIds)
}

// CreateSysRole 添加角色
func (s SysRoelServiceImpl) CreateSysRole(c *gin.Context, sysRole entity.StoreSysRoleDto) {
	roleName := dao.GetSysRoleByName(sysRole.RoleName, 0)
	if roleName.RoleId > 0 {
		response.Failed(c, int(constants.ApiCode.RoleNameAlreadyExists), constants.ApiCode.GetMessage(constants.ApiCode.RoleNameAlreadyExists))
		return
	}
	roleKey := dao.GetSysRoleByKey(sysRole.RoleKey, 0)
	if roleKey.RoleId > 0 {
		response.Failed(c, int(constants.ApiCode.RoleNameAlreadyExists), constants.ApiCode.GetMessage(constants.ApiCode.RoleNameAlreadyExists))
		return
	}
	result := dao.CreateSysRole(sysRole)
	if result {
		response.Success(c, nil)
		return
	}
	response.Failed(c, int(constants.ApiCode.FAILED), "添加失败")
}

// GetSysRoleById 角色详情
func (s SysRoelServiceImpl) GetSysRoleById(c *gin.Context, RoleId int) {
	row := dao.GetSysRoleById(RoleId)
	response.Success(c, row)
}

// UpdateSysRole 更新角色
func (s SysRoelServiceImpl) UpdateSysRole(c *gin.Context, sysRole entity.UpdateSysRoleDto) {
	roleName := dao.GetSysRoleByName(sysRole.RoleName, sysRole.RoleId)
	if roleName.RoleId > 0 {
		response.Failed(c, int(constants.ApiCode.RoleNameAlreadyExists), constants.ApiCode.GetMessage(constants.ApiCode.RoleNameAlreadyExists))
		return
	}
	roleKey := dao.GetSysRoleByKey(sysRole.RoleKey, sysRole.RoleId)
	if roleKey.RoleId > 0 {
		response.Failed(c, int(constants.ApiCode.RoleNameAlreadyExists), constants.ApiCode.GetMessage(constants.ApiCode.RoleNameAlreadyExists))
		return
	}
	result := dao.UpdateSysRole(sysRole)
	if result {
		response.Success(c, nil)
		return
	}
	response.Failed(c, int(constants.ApiCode.FAILED), "更新失败")
}

// UpdateSysRoleState 修改角色状态
func (s SysRoelServiceImpl) UpdateSysRoleState(c *gin.Context, sysRole entity.UpdateSysRoleStateDto) {
	result := dao.UpdateSysRoleState(sysRole)
	if result {
		response.Success(c, nil)
		return
	}
	response.Failed(c, int(constants.ApiCode.FAILED), "修改失败")
}

// DeleteSysRoleById 删除角色
func (s SysRoelServiceImpl) DeleteSysRoleById(c *gin.Context, dto entity.SysRoleIdDto) {
	err := dao.DeleteSysRole(dto)
	if err != nil {
		response.Failed(c, int(constants.ApiCode.FAILED), fmt.Sprintf("删除失败（%s）", err.Error()))
		return
	}
	response.Success(c, nil)
}

// AssignPermissions 给角色分配权限
func (s SysRoelServiceImpl) AssignPermissions(c *gin.Context, roleMenu entity.RoleMenu) {
	err := dao.AssignPermissions(roleMenu)
	if err != nil {
		response.Failed(c, int(constants.ApiCode.FAILED), fmt.Sprintf("权限分配失败（%s）", err.Error()))
		return
	}
	response.Success(c, nil)
}

var sysRoleService = SysRoelServiceImpl{}

func SysRoleService() ISysRoleService {
	return &sysRoleService
}
